// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <cstring>
#include <string>

int const maxn = 1100;
int bit[maxn];

int f[maxn][maxn];
int a[maxn][maxn];
int up[maxn][maxn];
int left[maxn][maxn];
int count[maxn];
std::string s;
int n, m;

void init()
{
	std::memset(count, 0, sizeof(count));
	std::memset(bit, 0, sizeof(bit));
}

int lowbit(int x) { return x &(-x); }

void bit_update(int id, int v)
{
	while (id <= n) {
		bit[id] += v;
		id += lowbit(id);
	}
}

int query(int id)
{
	int ret = 0;
	while (id > 0) {
		ret += bit[id];
		id -= lowbit(id);
	}
	return ret;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n >> m;
		init();
		for (int i = 1; i <= n; i++) {
			std::cin >> s;
			for (int j = 0; j < n; j++) a[i][j + 1] = (s[j] == '1');
		}

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				if (!a[i][j]) up[i][j] = left[i][j] = 0;
				else {
					up[i][j] = up[i - 1][j] + 1;
					left[i][j] = left[i][j - 1] + 1;
				}
			}

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				f[i][j] = std::min(f[i - 1][j - 1] + 1, std::min(left[i][j], up[i][j]));

				bit_update(1, 1);
				bit_update(f[i][j] + 1, -1);
			}
		}

		for (int i = 0, x; i < m; i++) {
			std::cin >> x;
			std::cout << query(x) << '\n';
		}
	}
}

